<template>
  <div ref="container">
    <div class="coupon-list" v-if="couponsList.length > 0">
      <div
        class="item acea-row row-center-wrapper"
        v-for="(item, index) in couponsList"
        :key="index"
      >
        <div class="money" :class="item.isUse ? 'moneyGray' : ''">
          <div>
            ￥<span class="num">{{ item.couponPrice }}</span>
          </div>
          <div class="pic-num">满{{ item.useMinPrice }}元可用</div>
        </div>
        <div class="text">
          <div class="condition line1">
           <span
                   class="line-title bg-color-check"
                   v-if="item.ctype === 0"
           >通用劵</span
           >
            <span
                    class="line-title bg-color-check"
                    v-else-if="item.ctype === 1"
            >商品券</span
            >
            <span
                    class="line-title bg-color-check"
                    v-else
            >未知</span
            >
            <span>{{ item.cname }}</span>
          </div>
          <div class="data acea-row row-between-wrapper">
            <div v-if="item.endTime !== 0">
              {{ item.startTime }}-{{ item.endTime }}
            </div>
            <div v-else>不限时</div>
            <div class="bnt gray" v-if="item.isUse === true">已领取</div>
            <div class="bnt gray" v-else-if="item.isUse === 2">已领完</div>
            <div
              class="bnt bg-color-red"
              v-else
              @click="getCoupon(item.id, index)"
            >
              立即领取
            </div>
          </div>
        </div>
      </div>
    </div>
    <Loading :loaded="loadend" :loading="loading"></Loading>
    <!--暂无优惠券-->
    <div
      class="noCommodity"
      v-cloak
      v-if="couponsList.length === 0 && page > 1"
    >
      <div class="noPictrue">
        <img src="@assets/images/noCoupon.png" class="image" />
      </div>
    </div>
  </div>
</template>
<script>
import { getCoupon, getCouponReceive } from "@api/user";
import Loading from "@components/Loading";
import { dataFormatT } from "@utils";
export default {
  name: "getCoupon",
  components: {
    Loading
  },
  props: {},
  data: function() {
    return {
      page: 1,
      limit: 10,
      couponsList: [],
      loading: false,
      loadend: false
    };
  },
  mounted: function() {
    this.getUseCoupons();
    this.$scroll(this.$refs.container, () => {
      !this.loading && this.getUseCoupons();
    });
  },
  methods: {
    dataFormatT,
    getCoupon: function(id, index) {
      let that = this;
      let list = that.couponsList;
      getCouponReceive(id)
        .then(function() {
          list[index].isUse = true;
          that.$dialog.toast({ mes: "领取成功" });
        })
        .catch(function(res) {
          that.$dialog.toast({ mes: res.msg });
        });
    },
    getUseCoupons: function() {
      let that = this;
      if (that.loading) return;
      if (that.loadend) return;
      that.loading = true;
      let q = { page: that.page, limit: that.limit };
      getCoupon(q).then(res => {
        that.loading = false;
        that.couponsList.push.apply(that.couponsList, res.data);
        that.loadend = res.data.length < that.limit; //判断所有数据是否加载完成；
        that.page = that.page + 1;
      });
    }
  }
};
</script>

<!--<style scoped>-->
  <!--.coupon-list .pic-num {-->
    <!--color: #ffffff !important;-->
    <!--font-size: 0.24rem !important;-->
  <!--}-->
<!--</style>-->
